package practice;

import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-09-06
 * Time: 8:03
 */
class Array{
    //创建数组
    int[] array = new int[1000_0000];
    Random random = new Random();
   public void setArray(){
       for (int i = 0; i < array.length; i++) {
           array[i] = random.nextInt(100)+1;//左闭右开区间
           //array[i] = i;
       }
   }

}
public class P1 {
    public static long sum1 = 0;
    public static long sum2 = 0;
    public static Array a = new Array();
    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        a.setArray();
        Thread t1 = new Thread(()->{
            a.array = Arrays.copyOf(a.array,a.array.length);
            for (int i = 0; i <a.array.length ; i+=2) {
               sum1+=a.array[i];
            }
            System.out.println("偶数下标元素的和sum1:"+sum1);
        },"线程1 计算偶数下标元素的和");

        Thread t2 = new Thread(()->{
            a.array = Arrays.copyOf(a.array,a.array.length);
            for (int j = 1; j <a.array.length ; j+=2) {
                sum2+=a.array[j];
                //sum2+=i;
            }
            System.out.println("奇数下标元素的和sum2:"+sum2);
        },"线程2 计算奇数下标元素的和");

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("sum1+sum2:"+(sum1+sum2));
        long endTime = System.currentTimeMillis();
        System.out.println("程序执行时间:"+(endTime-startTime));

    }



}
